﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using ARR.Entities;
using ARR.UI.ViewModels;
using ARR.UI.UnitsOfMeasureService;

namespace ARR.UI.Views
{
    /// <summary>
    /// Interaction logic for ArticleMasterJoinPopupView.xaml
    /// </summary>
    public partial class ArticleMasterJoinPopupView : Window
    {
        private List<Article> articles;
        private List<UnitOfMeasureConvertion> uomConvertions;
        
        public ArticleMasterJoinPopupView(List<Article> _articles)
        {
            InitializeComponent();
            this.articles = _articles;

            UnitsOfMeasureServiceClient service = new UnitsOfMeasureServiceClient();
            try
            {
                uomConvertions = service.GetAll();
                service.Close();
            }
            catch
            {
                service.Abort();
                throw;
            }

            ArticlesGV.ItemsSource = articles;
        }

        private void JoinButton_Click(object sender, RoutedEventArgs e)
        {
            if (this.ArticlesGV.SelectedItem != null)
            {
                Article selectedArticle = (Article)this.ArticlesGV.SelectedItem;
                List<Article> articles = this.ArticlesGV.Items.SourceCollection as List<Article>;
                
                string sourceUOM = articles.Where(x => x.Id != selectedArticle.Id).First().UOM;
                string targetUOM = selectedArticle.UOM;


                bool canJoin = true;
                if (sourceUOM != targetUOM)
                {
                    if (uomConvertions.Any(x => x.SourceUnit == sourceUOM && x.TargetUnit == targetUOM))
                    {
                        var uomc = uomConvertions.Single(x => x.SourceUnit == sourceUOM && x.TargetUnit == targetUOM);

                        (new Alert(string.Format("Se usara la siguiente conversion de UOM: {0}", uomc.DisplayName), "Unificación de Artículos", AlertType.Warning)).ShowDialog();                        
                    }
                    else
                    {
                        canJoin = false;
                        (new Alert("No existe conversion de unidad de medida entre las unidades de los articulos seleccionados.", "Unificación de Artículos", AlertType.Warning)).ShowDialog();
                    }
                }

                if (canJoin)
                {
                    bool? res = (new Confirm("Esta seguro que desea continuar? Esta accion no tiene vuelta atras")).ShowDialog();
                    if (res.HasValue && res.Value)
                    {
                        ArticlesService.ArticlesServiceClient service = new ArticlesService.ArticlesServiceClient();
                        try
                        {
                            service.Join(articles.Select(x => x.Id).ToList(), selectedArticle.Id);
                            service.Close();
                        }
                        catch
                        {
                            service.Abort();
                            throw;
                        }


                        this.DialogResult = true;
                        this.Close();
                    }
                }
                
            }
            else
            {
                (new Alert("Debe seleccionar el articulo destino!", "Unificación de Artículos", AlertType.Warning)).ShowDialog();
            }
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.Close();
        }
    }
}
